<?php

class Daftar_con extends CI_Controller {

    private $idterm;
    private $term;
    private $tahun;
    private $nama;

    function __construct() {
        parent::__construct();
        $query = $this->db->get_where('term', array('STATUS' => 'SEDANG'));
        $row = $query->row();
        $this->idterm = $row->ID_TERM;
        $this->term = $row->NO_TERM;
        $this->tahun = $row->TAHUN;
        session_start();
    }

    function index() {
        //menampilkan halaman login
        $this->form_login();
    }

    function form_login() {
        $display['noterm'] = $this->term;
        $display['thnterm'] = $this->tahun;
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/login_view', $display);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function validate_login() {
        $usr = $this->input->post('username');
        $password = $this->input->post('password');
        $strquery = "SELECT *
                FROM (
                user
                NATURAL JOIN role
                )
                NATURAL JOIN daftar_role
                WHERE USERNAME='" . $usr . "'
                AND PASSWORD='" . md5($password) . "'
                AND NAMA_ROLE='FO'    
                ";
        $query = $this->db->query($strquery);
        if ($query->num_rows() > 0) {
            $result = $query->row();
            $this->nama = $result->NAMA;
            $data = array(
                'Loggedin' => TRUE,
                'Nama' => $result->NAMA,
                'ID' => $result->ID
            );
            $this->session->set_userdata($data);


            $display['nama'] = $this->session->userdata('Nama');
            $display['noterm'] = $this->term;
            $display['thnterm'] = $this->tahun;

            $id = $this->session->userdata('ID');
            $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=" . $id . " AND ID_TERM=" . $this->idterm;
            $query = $this->db->query($strquery);
            $display['jumlah'] = $query->row()->jumlah;

            $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_TERM=" . $this->idterm;
            $query = $this->db->query($strquery);
            $display['total'] = $query->row()->jumlah;
            $display['hasil'] = $this->db->query('select * from pelatihan natural join kelas')->result_array();

            $_SESSION['display'] = $display;


            redirect(base_url() . 'pendaftaran/daftar_con/home');
        } else {
            redirect(base_url() . 'pendaftaran');
        }
    }

    function logout() {
        $data = array(
            'Loggedin' => FALSE,
            'Nama' => '',
            'ID' => ''
        );
        $this->session->unset_userdata($data);
        redirect(base_url() . 'pendaftaran');
    }

    function home() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/home_view', $_SESSION['display']);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function list_pendaftar($kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $hasil = $this->db->query('select * from pelatihan natural join kelas where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->row_array();
        $_SESSION['display']['namapelatihan'] = $hasil['NAMA_PELATIHAN'];
        $_SESSION['display']['kode'] = $kode;
        $_SESSION['display']['angkatan'] = $angkatan;
        $_SESSION['display']['kuota'] = $hasil['KUOTA'];
        $_SESSION['display']['harga'] = $hasil['HARGA'];

        $strqueryhari = "SELECT DISTINCT HARI FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $_SESSION['display']['kode'] . "' AND ANGKATAN=" . $_SESSION['display']['angkatan'] . " ORDER BY HARI";
        $reshari = $this->db->query($strqueryhari)->result_array();

        for ($i = 0; $i < count($reshari); $i++) {
            $strquerywaktumin = "SELECT MIN(KODE_WAKTU) as Min FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $_SESSION['display']['kode'] . "' AND ANGKATAN =" . $_SESSION['display']['angkatan'] . " AND HARI=" . $reshari[$i]['HARI'];
            $waktumin = $this->db->query($strquerywaktumin)->row()->Min;
            $strquerywaktumax = "SELECT MAX(KODE_WAKTU) as Max FROM jadwal_pelatihan WHERE KODE_PELATIHAN = '" . $_SESSION['display']['kode'] . "' AND ANGKATAN =" . $_SESSION['display']['angkatan'] . " AND HARI=" . $reshari[$i]['HARI'];
            $waktumax = $this->db->query($strquerywaktumax)->row()->Max;
            $_SESSION['display']['waktu'][$i]['hari'] = $this->konversi_hari($reshari[$i]['HARI']);
            $_SESSION['display']['waktu'][$i]['jam'] = $this->konversi_kode_waktu($waktumin, $waktumax);
        }

        $jum = $this->db->query('select count(ID_USER) as jum from peserta_kelas where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->row();
        $_SESSION['display']['jumlah'] = $jum->jum;

        $_SESSION['display']['pendaftar'] = $this->db->query('select * from user inner join peserta_kelas on user.ID=peserta_kelas.ID_USER where ANGKATAN=' . $angkatan . ' and KODE_PELATIHAN="' . $kode . '"')->result_array();

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/pendaftar_view', $_SESSION['display']);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/pilih_daftar_view', $_SESSION['display']);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_new() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $id = $this->session->userdata('ID');
        $strquery = "SELECT DISTINCT count(ID_USER) as jumlah FROM peserta_kelas WHERE ID_FO=" . $id;
        $query = $this->db->query($strquery);
        $_SESSION['display']['jumlah'] = $query->row()->jumlah;
        $query = $this->db->get('kelas');
        $_SESSION['display']['pelatihan'][''] = '';
        foreach ($query->result() as $entry) {
            $kode = $entry->KODE_PELATIHAN . '-' . $entry->ANGKATAN;
            $_SESSION['display']['pelatihan'][$kode] = $kode;
        }
        $query = $this->db->get('diskon');
        $i = 0;
        foreach ($query->result() as $entry) {
            $_SESSION['display']['diskon'][$i]['no'] = $entry->ID_DISKON;
            $_SESSION['display']['diskon'][$i]['nama'] = $entry->NAMA_DISKON;
            $i++;
        }
        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/form_new_view', $_SESSION['display']);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_new_proses() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $nama = $this->input->post('nama');
        $nohp = $this->input->post('nohp');
        $gol = $this->input->post('gol');
        $kelas = $this->input->post('kelas');
        $username = str_ireplace(' ', '.', strtolower($nama));
        $password = md5($username);
        $data = array(
            'NAMA' => $nama,
            'NO_TELP' => $nohp,
            'GOL' => $gol,
            'USERNAME' => $username,
            'PASSWORD' => $password
        );

        $this->db->insert('user', $data);
        $strquery = "select ID from user where USERNAME='" . $username . "'";

        $id = $this->db->query($strquery)->row()->ID;

        $id_role = $this->db->get_where('daftar_role', array('NAMA_ROLE' => 'Peserta'))->row()->ID_ROLE;

        $inrole = array('ID' => $id, 'ID_ROLE' => $id_role);
        $this->db->insert('role', $inrole);

        $besar_diskon = 0;

        $no = $this->input->post('nocic');
        $num_rows = '';
        if ($no != '') {
            $query = $this->db->get_where('peserta', array(
                        'NO_CIC_CARD' => $no, 'STATUS_CIC_CARD' => 1
                    ));
            $num_rows = $query->num_rows();
            if ($query->num_rows() == 1) {
                $besar_diskon = 20;
                $update = array('STATUS_CIC_CARD' => 0);
                $this->db->where('NO_CIC_CARD', $no);
                $this->db->update('peserta', $update);
            } else {
                //diskon fail
                $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="javascript:back()">Kembali</a>';
                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas',$_SESSION['display']);
                $this->load->view('pendaftaran/pesan_kesalahan', $data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }
        } else {
            //ngecek diskon yg dicentang
            $diskon = $this->input->post('diskon');
            if (!empty($diskon)) {
                for ($i = 0; $i < count($diskon); $i++) {
                    $hasil = $this->db->get_where('diskon', array('ID_DISKON' => $diskon[$i]))->row();
                    if (strcasecmp($hasil->NAMA_DISKON, 'diskon golongan') == 0) {
                        switch ($gol) {
                            case 1:
                                $besar_diskon+=10;
                                break;
                            case 2:
                                $besar_diskon+=10;
                                break;
                            case 3:
                                $besar_diskon+=5;
                                break;
                            case 4:
                                $besar_diskon+=0;
                                break;
                            case 5:
                                $besar_diskon+=100;
                                break;
                        }
                    } else {
                        $besar_diskon+=$hasil->BESAR;
                    }
                }
            }
        }
        if ($num_rows == 1 || $no == '') {
            $pelatihan = explode('-', $kelas);
            $kode = $pelatihan[0];
            $angkatan = $pelatihan[1];
            $result = $this->db->get_where('kelas', array(
                        'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row();
            $harga = $result->HARGA;
            if ($besar_diskon > 100)
                $besar_diskon = 100;

            $temp = (100 - $besar_diskon) / 100 * $harga;
            $total_bayar = $this->division2($temp, 1000);
            $data = array(
                'ID_USER' => $id,
                'ANGKATAN' => $angkatan,
                'KODE_PELATIHAN' => $kode,
                'TOTAL_BAYAR' => $total_bayar,
                'TGL_DAFTAR' => date('Y-m-d'),
                'ID_TERM' => $this->idterm,
                'ID_FO' => $this->session->userdata('ID')
            );
            $this->db->insert('peserta_kelas', $data);
            $nourut = $this->cari_urut();
            $gen_nocic = $this->convert_no($nourut) . '-' . $this->cut_year($this->tahun) . $gol;
            $data = array('NO_CIC_CARD' => $gen_nocic, 'ID' => $id, 'STATUS_CIC_CARD' => 'FALSE');
            $this->db->insert('peserta ', $data);
            $data['id'] = $id;
            $data['nama'] = $nama;
            $data['nohp'] = $nohp;
            $data['no_cic'] = $gen_nocic;
            $data['kelas'] = $kelas;
            $data['pelatihan'] = $kode;
            $data['angkatan'] = $angkatan;
            $data['biaya'] = $harga;
            $data['diskon'] = $besar_diskon;
            $data['bayar'] = $total_bayar;

            $result = $this->db->query('select count(JUMLAH) as total from pembayaran where ID=' . $id)->row();
            $data['sudah_bayar'] = $result->total;
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
            $this->load->view('pendaftaran/biaya_view', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
    }

    function cari_urut() {
        $query = $this->db->query('SELECT count(*) as jumlah FROM peserta');
        $result = $query->result_array();
        return ($result[0]['jumlah'] + 1);
    }

    function display_biaya($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];

        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];
        $data['pelatihan'] = $pelatihan;
        $data['angkatan'] = $angkatan;
        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

        $potongan = $data['biaya'] - $data['bayar'];

        $data['diskon'] = round($potongan * 100 / $data['biaya']);

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/biaya_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function bayar($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];

        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

        $potongan = $data['biaya'] - $data['bayar'];

        $data['diskon'] = round($potongan * 100 / $data['biaya']);

        $this->db->order_by('TGL_BAYAR', 'asc');
        $data['history'] = $this->db->get_where('pembayaran', array('ID' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->result_array();


        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/bayar_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function input_bayar($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $besar_bayar = $this->input->post('besar');
        $tgl = date('Y-m-d H:i:s');

        $data = array(
            'ID' => $id,
            'TGL_BAYAR' => $tgl,
            'JUMLAH' => $besar_bayar,
            'ID_FO' => $this->session->userdata('ID'),
            'KODE_PELATIHAN' => $kode,
            'ANGKATAN' => $angkatan
        );
        $this->db->insert('pembayaran', $data);

        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];

        $data['tgl'] = $tgl;
        $data['besar'] = $besar_bayar;

        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];
        $data['kode'] = $pelatihan;
        $data['angkatan'] = $angkatan;
        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/struk_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function convert_no($no) {
        $strno = '' . $no;
        for ($i = 0; $i < 5 - strlen($strno); $i++) {
            $strno = '0' . $strno;
        }
        return $strno;
    }

    function cut_year($year) {
        $stryear = '' . $year;
        return substr($stryear, 2);
    }

    function daftar_old() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/cari_nocic');
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function cari_user() {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $nocic = $this->input->post('nocic');
        $strquery = 'select * from peserta inner join user on peserta.ID=user.ID where peserta.NO_CIC_CARD="' . $nocic . '"';
        $query = $this->db->query($strquery);
        //echo $nocic.' '.$strquery;
        if ($query->num_rows == 1) {
            $hasil = $query->row_array();
            $data['id'] = $hasil['ID'];
            $data['nama'] = $hasil['NAMA'];
            $data['nocic'] = $hasil['NO_CIC_CARD'];
            $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan <br/> <a href="' . base_url() . 'pendaftaran/daftar_con/daftar">Daftar baru</a>';
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
            $this->load->view('pendaftaran/hasilcari_view', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        } else {
            $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan <br/> <a href="' . base_url() . 'pendaftaran/daftar_con/daftar">Daftar baru</a>';
            $this->load->view('pendaftaran/daftar_header_view');
            $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
            $this->load->view('pendaftaran/pesan_kesalahan', $data);
            $this->load->view('pendaftaran/daftar_footer_view');
        }
    }

    function form_daftar_old($id) {

        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $query = $this->db->get('kelas');
        $data['pelatihan'][''] = '';
        foreach ($query->result() as $entry) {
            $kode = $entry->KODE_PELATIHAN . '-' . $entry->ANGKATAN;
            $data['pelatihan'][$kode] = $kode;
        }
        $query = $this->db->get('diskon');
        $i = 0;
        foreach ($query->result() as $entry) {
            $data['diskon'][$i]['no'] = $entry->ID_DISKON;
            $data['diskon'][$i]['nama'] = $entry->NAMA_DISKON;
            $i++;
        }

        $data['user'] = $this->db->query('select * from user where ID=' . $id)->row_array();
        $data['peserta'] = $this->db->query('select * from peserta where ID=' . $id)->row_array();

        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/form_old_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function daftar_old_proses() {

        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $nama = $this->input->post('nama');
        $nohp = $this->input->post('nohp');
        $gol = $this->input->post('gol');
        $kelas = $this->input->post('kelas');

        $besar_diskon = 0;

        $no = $this->input->post('nocic');

        if ($no != '') {
            $query = $this->db->get_where('peserta', array(
                        'NO_CIC_CARD' => $no, 'STATUS_CIC_CARD' => 1
                    ));
            if ($query->num_rows() == 1) {
//                    echo $besar_diskon;
//            die($besar_diskon);
                $besar_diskon = 25;
                $update = array('STATUS_CIC_CARD' => 0);
                $this->db->where('NO_CIC_CARD', $no);
                $this->db->update('peserta', $update);
            } else {
                //diskon fail
                $data['pesan'] = 'No CIC anda sudah tidak dapat digunakan lagi <br/> <a href="javascript:back()">Kembali</a>';
                $this->load->view('pendaftaran/daftar_header_view');
                $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
                $this->load->view('pendaftaran/pesan_kesalahan', $data);
                $this->load->view('pendaftaran/daftar_footer_view');
            }
        } else {
            //ngecek diskon yg dicentang
            $diskon = $this->input->post('diskon');
            if (!empty($diskon)) {
                for ($i = 0; $i < count($diskon); $i++) {
                    $hasil = $this->db->get_where('diskon', array('ID_DISKON' => $diskon[$i]))->row();
                    if (strcasecmp($hasil->NAMA_DISKON, 'diskon golongan') == 0) {
                        switch ($gol) {
                            case 1:
                                $besar_diskon+=10;
                                break;
                            case 2:
                                $besar_diskon+=10;
                                break;
                            case 3:
                                $besar_diskon+=5;
                                break;
                            case 4:
                                $besar_diskon+=0;
                                break;
                            case 5:
                                $besar_diskon+=100;
                                break;
                        }
                    } else {
                        $besar_diskon+=$hasil->BESAR;
                    }
                }
            }
        }

        $pelatihan = explode('-', $kelas);
        $kode = $pelatihan[0];
        $angkatan = $pelatihan[1];
        $result = $this->db->get_where('kelas', array(
                    'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row();
        $harga = $result->HARGA;
        $tb = (100 - $besar_diskon) / 100 * $harga;
	 $total_bayar = $this->division2($tb,1000);
        $id = $this->input->post('id');
        $data = array(
            'ID_USER' => $id,
            'ANGKATAN' => $angkatan,
            'KODE_PELATIHAN' => $kode,
            'TOTAL_BAYAR' => $total_bayar,
            'TGL_DAFTAR' => date('Y-m-d'),
            'ID_TERM' => $this->idterm,
            'ID_FO' => $this->session->userdata('ID')
        );
        $this->db->insert('peserta_kelas', $data);
        redirect(base_url() . 'pendaftaran/daftar_con/display_biaya/' . $id . '/' . $kode . '/' . $angkatan);
    }

    function history($id, $kode, $angkatan) {
        if ($this->session->userdata('Loggedin') == FALSE) {
            redirect(base_url() . 'pendaftaran');
        }

        $user = $this->db->get_where('user', array('ID' => $id))->row_array();
        $data['id'] = $id;
        $data['nama'] = $user['NAMA'];
        $data['nohp'] = $user['NO_TELP'];
        $peserta = $this->db->get_where('peserta', array('ID' => $id))->row_array();
        $data['no_cic'] = $peserta['NO_CIC_CARD'];
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $peserta_kelas = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->row_array();

        $data['bayar'] = $peserta_kelas['TOTAL_BAYAR'];

        $pelatihan = $peserta_kelas['KODE_PELATIHAN'];
        $angkatan = $peserta_kelas['ANGKATAN'];

        $data['kelas'] = $pelatihan . '-' . $angkatan;

        $kelas = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $pelatihan, 'ANGKATAN' => $angkatan))->row_array();
        $data['biaya'] = $kelas['HARGA'];

        $result = $this->db->query('select sum(JUMLAH) as total from pembayaran where ID=' . $id . ' and KODE_PELATIHAN="' . $kode . '" and ANGKATAN=' . $angkatan)->row();
        $data['sudah_bayar'] = $result->total;

        $potongan = $data['biaya'] - $data['bayar'];

        $data['diskon'] = $potongan * 100 / $data['biaya'];

        $this->db->order_by('TGL_BAYAR', 'asc');
        $data['history'] = $this->db->get_where('pembayaran', array('ID' => $id, 'KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan))->result_array();


        $this->load->view('pendaftaran/daftar_header_view');
        $this->load->view('pendaftaran/menu_atas', $_SESSION['display']);
        $this->load->view('pendaftaran/history_view', $data);
        $this->load->view('pendaftaran/daftar_footer_view');
    }

    function konversi_hari($nomor) {
        switch ($nomor) {
            case 1: return 'Senin';
            case 2: return 'Selasa';
            case 3: return 'Rabu';
            case 4: return 'Kamis';
            case 5: return 'Jumat';
            case 6: return 'Sabtu';
            case 7: return 'Minggu';
        }
    }

    function division($bil1, $bil2) {
        $temp = ($bil1 - 1) / $bil2;
        $temp2 = ($bil1 - 1) % $bil2;
        return ($temp - ($temp2 / $bil2));
    }

    function division2($bil1='', $bil2='') {

        $hasil = $bil1 / $bil2;
        $hasil = round($hasil);
        return 1000 * $hasil;
    }

    function isgenap($nomor) {
        return (($nomor % 2) == 0);
    }

    function konversi_kode_waktu($nomor1, $nomor2) {
        $hasil = 15 + $this->division($nomor1, 2);
        $hasil .= '.';
        if (!$this->isgenap($nomor1))
            $hasil .= '00';
        else
            $hasil .= '30';
        $hasil .= '-';

        if ($this->isgenap($nomor2)) {
            $hasil .= 15 + $this->division($nomor2, 2) + 1;
            $hasil .= '.';
            $hasil .= '00';
        } else {
            $hasil .= 15 + $this->division($nomor2, 2);
            $hasil .= '.';
            $hasil .= '30';
        }
        return $hasil;
    }

    function konversi_rupiah($angka) {
        
    }

    function cetak_kuitansi($id='', $kode='', $angkatan='') {
        $data['kode'] = $kode;
        $data['angkatan'] = $angkatan;
        $result = $this->db->get_where('user', array('ID' => $id));
        $array = $result->result_array();
        $data['nama_peserta'] = $array[0]['NAMA'];
        $data['tanggal'] = date('d m Y H.i');
        $data['nama_petugas'] = $this->session->userdata('Nama');
        $data['no_hp_peserta'] = $array[0]['NO_TELP'];
        $result = $this->db->get_where('peserta', array('ID' => $id));
        $array = $result->result_array();
        $data['no_cic_peserta'] = $array[0]['NO_CIC_CARD'];
        $result = $this->db->get_where('pelatihan', array('KODE_PELATIHAN' => $kode));
        $array = $result->result_array();
        $data['nama_pelatihan'] = $array[0]['NAMA_PELATIHAN'];
        $result = $this->db->get_where('kelas', array('KODE_PELATIHAN' => $kode, 'ANGKATAN' => $angkatan));
        $array = $result->result_array();
        $data['jam_total'] = $array[0]['JAM_TOTAL'];
        $data['pertemuan'] = $array[0]['JAM_TOTAL'] / $array[0]['JAM_PER_SESI'];
        $data['term'] = $array[0]['TERM'];
        $data['harga'] = $array[0]['HARGA'];
        $data['harga2'] = $this->konversi_rupiah($data['harga'] . '');
        $data['sesi_per_minggu'] = $array[0]['SESI_PER_MINGGU'];
        $result = $this->db->get_where('term', array('NO_TERM' => $data['term']));
        $array = $result->result_array();
        $data['tahun_term'] = $array[0]['TAHUN'];
        $result = $this->db->query('SELECT ID_RUANGAN, HARI
                                        FROM  jadwal_pelatihan
                                        WHERE KODE_PELATIHAN="' . $kode . '"
                                        AND ANGKATAN = ' . $angkatan . '
                                        GROUP BY  HARI
                                        LIMIT 0 , 30');
        $array = $result->result_array();
        $hari = array();
        $jam = array();
        for ($i = 0; $i < count($array); $i++) {
            $hari[$i] = $this->konversi_hari($array[$i]['HARI']);
            $result = $this->db->query('SELECT MIN(KODE_WAKTU) AS min,MAX(KODE_WAKTU) as max FROM `jadwal_pelatihan` WHERE HARI =' . $array[$i]['HARI'] . ' AND KODE_PELATIHAN="' . $kode . '"
                                        AND ANGKATAN = ' . $angkatan);
            $array2 = $result->result_array();
            $jam[$i] = $this->konversi_kode_waktu($array2[0]['min'], $array2[0]['max']);
        }
        $data['hari'] = $hari;
        $data['jam'] = $jam;

        $result = $this->db->query('SELECT *
                                        FROM  pembayaran WHERE ID=' . $id . ' AND
                                        KODE_PELATIHAN = "' . $kode . '" AND
                                        ANGKATAN = ' . $angkatan . '
                                        ORDER BY  TGL_BAYAR DESC ');
        $array = $result->result_array();
        $data['jumlah_pembayaran'] = $array[0]['JUMLAH'];
        $data['tanggal_pembayaran'] = substr($array[0]['TGL_BAYAR'], 0, 10);
        $result = $this->db->get_where('peserta_kelas', array('ID_USER' => $id, 'ANGKATAN' => $angkatan, 'KODE_PELATIHAN' => $kode));
        $array = $result->result_array();
        $data['total_bayar'] = $array[0]['TOTAL_BAYAR'];
        $result = $this->db->query('SELECT count(*)AS hitung,SUM(JUMLAH) AS jumlah
                                        FROM  pembayaran WHERE ID=' . $id . ' AND
                                        KODE_PELATIHAN = "' . $kode . '" AND
                                        ANGKATAN = ' . $angkatan . '
                                        ORDER BY  TGL_BAYAR DESC ');
        $array = $result->result_array();
        $data['pembayaran_ke'] = $array[0]['hitung'];
        $data['sisa_pembayaran'] = $data['total_bayar'] - $array[0]['jumlah'];
        $data['diskon'] = round(($data['harga'] - $data['total_bayar']) / $data['harga'] * 100);


        $this->load->view('pendaftaran/print', $data);
    }

}

?>
